import * as THREE from '../../build/three.module';
import { drawThreeGeo, drawModel, drawLine } from './threeGeoJSON.js';
import { util } from './util';
import * as d3 from 'd3-geo';
const RADIUS = 50;

class MeshHelper {

    lnglatToMector(lnglat) {
        if (!this.projection) {
            this.projection = d3
                .geoMercator()
                .center([108.904496, 32.668849])
                .scale(80)
                .rotate(Math.PI / 4)
                .translate([0, 0]);
        }
        const [y, x] = this.projection([...lnglat]);
        let z = 105;
        return [x, y, z];
    }

    

    /**
     * @desc 创建中国地块
     * @param {object} geojs 
     */
    createMeshChina(chinaData, worldMesh) {

        drawThreeGeo(chinaData, RADIUS + 0.5, 'sphere', {
            color: 0x00ff00,
            transparent: false
        }, worldMesh);


        //二维中国地图块
        // 把经纬度转换成x,y,z 坐标
        // chinaData.features.forEach(d => {
        //     d.vector3 = [];
        //     d.geometry.coordinates.forEach((coordinates, i) => {
        //         d.vector3[i] = [];
        //         coordinates.forEach((c, j) => {
        //             if (c[0] instanceof Array) {
        //                 d.vector3[i][j] = [];
        //                 c.forEach(cinner => {
        //                     let cp = this.lnglatToMector(cinner);
        //                     d.vector3[i][j].push(cp);
        //                 });
        //             } else {
        //                 let cp = this.lnglatToMector(c);
        //                 d.vector3[i].push(cp);
        //             }
        //         });
        //     });
        // });
        // // 绘制地图模型
        // const group = new THREE.Group();
        // const lineGroup = new THREE.Group();
        // chinaData.features.forEach(d => {
        //     const g = new THREE.Group(); // 用于存放每个地图模块。||省份
        //     g.data = d;
        //     d.vector3.forEach(points => {
        //         // 多个面
        //         if (points[0][0] instanceof Array) {
        //             points.forEach(p => {
        //                 const mesh = drawModel(p);
        //                 const lineMesh = drawLine(p);
        //                 lineGroup.add(lineMesh);
        //                 g.add(mesh);
        //             });
        //         } else {
        //             // 单个面
        //             const mesh = drawModel(points);
        //             const lineMesh = drawLine(points);
        //             lineGroup.add(lineMesh);
        //             g.add(mesh);
        //         }
        //     });
        //     group.add(g);
        // });
        // earth.groupMapCn = group; // 丢到全局去
        // const lineGroupBottom = lineGroup.clone();
        // lineGroupBottom.position.z = -2;
        // earth.scene.add(lineGroup);
        // earth.scene.add(lineGroupBottom);
        // earth.scene.add(group);
        // lineGroup.position.x = -10;lineGroup.position.y = 50; lineGroup.position.z = 20;
        // lineGroup.rotation.x=-1,lineGroup.rotation.y=0,lineGroup.rotation.z =1.50;

        //      earth.groupMapCn.position.x = -10;earth.groupMapCn.position.y = 50; earth.groupMapCn.position.z = 20;
        //    earth.groupMapCn.rotation.x=-1,earth.groupMapCn.rotation.y=0,earth.groupMapCn.rotation.z =1.50;

    }

    createMeshWorld(geojs) {
        let material = new THREE.MeshBasicMaterial({
            color: new THREE.Color("rgb(94, 153, 195)"),
        });
        let geometry = new THREE.SphereGeometry(RADIUS - 1, 42, 42)
        let mesh = new THREE.Mesh(geometry, material);
        mesh.rotation.y = Math.PI / 2;
        mesh.rotation.x = Math.PI / 2;


        // drawThreeGeo(geojs, RADIUS + 0.5, 'sphere', {
        //     color: 0x00ff00,
        //     transparent: false
        // }, mesh);
        return mesh;
    }

}

export const meshHelper = new MeshHelper();